// accounting/cashier_account/cashier_account.js
let listBehavior = require('../../behavior/list')
Page({
  behaviors: [ listBehavior, ],

  /**
   * 页面的初始数据
   */
  data: {
    showDrop: false,
    list: []

  },
  bindShowDrop(e){
    let {index} = e.currentTarget.dataset;
    this.setData({
      [`list[${index}].showDrop`]: !this.data.list[index].showDrop
    })
  },
  hideDrop(e){
    let {list} = this.data;
    wx.$each(list,(i,item)=>{
      if (item.showDrop){
        item.showDrop = false;
      }
    });
    this.setData({list});
  },
  onDelete(e){
    if (!this.checkAuth('tableDelete')) return;
    wx.showModal({
      title: '提示',
      content: '确认删除该账户？',
      success: (res) => {
        if(res.confirm) {
          let {number, index } = e.currentTarget.dataset;
          wx.$post(3,'CashierAccount/del_all_cashier_account',{number}).then(res=>{
            if (res.errcode != 0)return;
            let list = this.data.list;
            list.splice(index, 1);
            wx.$toast('删除成功！');
            this.setData({
              list
            });
          });
        }
      },
    })

  },
  navTo(e){
    let {url,type} = e.currentTarget.dataset;
    if (type === 'add' && !this.checkAuth('tableAppend')) return;
    if (type === 'edit' && !this.checkAuth('tableEdit')) return;
    wx.navigateTo({
      url
    })
  },
  getDataList(num){
    return new Promise(resolve => {
      let post = {"limit":0,"page":1,"oby":"account_id"};
      if (num){
        post.SearchWhere = [{
          CompareValue: num,
          CompareWith: "number",
          Operator: "=",
          or: 0
        }]
      }
      wx.$post(3,'CashierAccount/query_all_cashier_account',post).then(res=>{
        if (res.errcode != 0)return;
        resolve(res.data.row || []);
      })
    })
  },
  checkAuth(type){
    let flag = false,
      obj = {
        tableAppend: '抱歉，您没有权限新增该模块数据！',
        tableEdit: '抱歉，您没有权限编辑该模块数据！',
        tableDelete: '抱歉，您没有权限删除该模块数据！',
      };
    flag = this.data.auth[type];
    if (!flag){
      wx.$toast(obj[type]);
    }
    return flag;
  },
  refreshData(num,index){
    if (!num){
      this.getDataList().then(list=>{
        this.setData({list,limit: list.length,total: list.length});
      });
      return
    }
    let {list} = this.data;
    this.getDataList(num).then(data=>{
      list[index] = data[0];
      this.setData({
        list
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.$setBarTitle();
    wx.$getAuth('all_cashier_account').then(res=>{
      if (!res.tableSee) {
        wx.$toast('抱歉，您没有权限查看该模块数据！',1000).then(()=>{
          wx.navigateBack({ delta:1 });
        });
        return
      }
      this.setData({
        auth: res
      });
      this.getDataList().then(list=>{
        this.setData({list,limit: list.length,total: list.length});
      });
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})
